package com.itheima.d10_observer;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 具备被观察者
 */
public class Home implements IHomePrice {

    private double price;

    public Home(double price) {
        this.price = price;
    }

    public double getPrice() {
        return price;
    }

    //修改房价
    public void setPrice(double price) {
        this.price = price;
        notifyObserver( price ); //房价变更，通知所有关注了此房价的观察者
    }

    // 被观察者维护一个观察者集合：方便后续通知
    Set<INotice> observers = new HashSet<>();


    @Override
    public void registerObserver(INotice notice) {
        observers.add( notice ); //订阅/关注
    }


    @Override
    public void removeObserver(INotice notice) {
        observers.remove( notice ); //取消关注
    }

    @Override
    public void notifyObserver(double price) {
        observers.forEach( notice -> notice.update( price ));
    }
}
